package com.elong.businesstravel.modules.member;

import android.R.integer;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.baseactivity.BaseActivity;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.entitys.MemberManaged;
import com.elong.businesstravel.protocol.login.MemberGetManagedRequest;
import com.elong.businesstravel.protocol.login.MemberGetManagedResponse;

/**
 * 会员级别
 * 
 * @author lenovo
 * 
 */
public class MemberLeveActivity extends BaseActivity {

	private TextView mMemberLevelDescTextView;
	private ViewGroup mMemeberLevelDescLayout;

	private MemberManaged mMemberManaged;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_member_leve);

		init();
	}

	@Override
	protected void init() {

		if (null == mSavedInstanceState) {
			mMemberManaged = (MemberManaged) getIntent().getSerializableExtra(
					"MemberManaged");
		} else {
			mMemberManaged = (MemberManaged) mSavedInstanceState
					.getSerializable("MemberManaged");
		}

		mMemberLevelDescTextView = (TextView) findViewById(R.id.memberLevelDescTextView);
		mMemeberLevelDescLayout = (ViewGroup) findViewById(R.id.memeberLevelDescLayout);

		ViewGroup upgradeMemberLevelLayout = (ViewGroup) findViewById(R.id.upgradeMemberLevelLayout);
		upgradeMemberLevelLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext, WebViewActivity.class);
				intent.putExtra("title", "如何提升等级");
				intent.putExtra("url",
						"file:///android_asset/html/uplevel.html");
				startActivity(intent);
			}
		});

		if (null != mMemberManaged) {
			int level = 0;
			try {
				level = Integer.parseInt(mMemberManaged.ulevel);
			} catch (Exception e) {
			}
			setMemberLevel(level);
			if (TextUtils.isEmpty(mMemberManaged.text)) {
				mMemeberLevelDescLayout.setVisibility(View.GONE);
				mMemberLevelDescTextView.setVisibility(View.GONE);
			} else {
				mMemeberLevelDescLayout.setVisibility(View.VISIBLE);
				mMemberLevelDescTextView.setVisibility(View.VISIBLE);
				mMemberLevelDescTextView.setText(mMemberManaged.text);
			}
		} else {
			memberGetManaged();
		}

		setTitleNavBar();

	}

	@Override
	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage("会员等级");
		titleNavBarView.setLeftDrawableImage(R.drawable.btn_title_back);
		titleNavBarView.setLeftButtonImageString(0, "返回", R.color.text_blue);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	/**
	 * 设置会员级别
	 * 
	 * @param level
	 *            0普通，1VIP，2钻石
	 */
	private void setMemberLevel(int level) {
		TextView memberLevelTextView = (TextView) findViewById(R.id.memberLevelTextView);
		TextView putongFlagTextView = (TextView) findViewById(R.id.putongFlagTextView);
		TextView vipFlagTextView = (TextView) findViewById(R.id.vipFlagTextView);
		TextView diamondFlagTextView = (TextView) findViewById(R.id.diamondFlagTextView);
		if (0 == level) {
			memberLevelTextView.setText("普通");
			putongFlagTextView.setBackgroundColor(getResources().getColor(
					R.color.bg_member_level_flag));
			vipFlagTextView.setBackgroundColor(getResources().getColor(
					R.color.bg_layout_gray));
			diamondFlagTextView.setBackgroundColor(getResources().getColor(
					R.color.bg_layout_gray));
		} else if (1 == level) {
			memberLevelTextView.setText("VIP");
			putongFlagTextView.setBackgroundColor(getResources().getColor(
					R.color.bg_layout_gray));
			vipFlagTextView.setBackgroundColor(getResources().getColor(
					R.color.bg_member_level_flag));
			diamondFlagTextView.setBackgroundColor(getResources().getColor(
					R.color.bg_layout_gray));
		} else if (2 == level) {
			memberLevelTextView.setText("钻石");
			putongFlagTextView.setBackgroundColor(getResources().getColor(
					R.color.bg_layout_gray));
			vipFlagTextView.setBackgroundColor(getResources().getColor(
					R.color.bg_layout_gray));
			diamondFlagTextView.setBackgroundColor(getResources().getColor(
					R.color.bg_member_level_flag));
		}
	}

	private void memberGetManaged() {
		NetworkClient.getNetworkClient().PostRequest(
				new MemberGetManagedRequest(mContext),
				new MemberGetManagedResponse() {

					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						HandlerPostUI.getHandlerPostUI(new Runnable() {

							@Override
							public void run() {
								mMemberManaged = memberManaged;
								int level = 0;
								try {
									level = Integer
											.parseInt(mMemberManaged.ulevel);
								} catch (Exception e) {
								}
								setMemberLevel(level);
								mMemberLevelDescTextView
										.setText(mMemberManaged.text);
							}
						});
					}

				});
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putSerializable("MemberManaged", mMemberManaged);
		super.onSaveInstanceState(outState);
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		if(null != savedInstanceState){
			mMemberManaged = (MemberManaged) savedInstanceState
					.getSerializable("MemberManaged");
		}
	}

}
